﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.BZip2;


namespace AppEntity
{
    public partial class Common
    {
        /// <summary>
        /// http://devpinoy.org/blogs/keithrull/archive/2008/01/25/how-to-create-zip-files-in-c-with-sharpziplib-ziplib.aspx
        /// </summary>
        public class Zip
        {
            private ZipFile _z;
            public Zip(Stream OutPutStream)
            {
                this._z = ZipFile.Create(OutPutStream);
                this._z.BeginUpdate();
            }
            public Zip(string FileFullName)
            {
                this._z = ZipFile.Create(FileFullName);
                this._z.BeginUpdate();
            }
            /// <summary>
            /// 设置压缩文档的访问密码
            /// </summary>
            /// <param name="pwd">密码明文</param>
            public void SetPassword(string pwd)
            {
                this._z.Password = pwd;
            }
            /// <summary>
            /// 添加文件到压缩文档
            /// </summary>
            /// <param name="FileFullName">文件的完整路径</param>
            /// <param name="EntryName">文件在压缩文档里面的路径，如：abc\\def.txt，目标文件将被命名为def.txt 并存放于abc文件夹下</param>       
            public void AddFile(string FileFullName, string EntryName)
            {
                this._z.Add(FileFullName, EntryName);
            }
            public void AddFile(string FileFullName)
            {
                this._z.Add(FileFullName);
            }
            /// <summary>
            /// 添加文件到压缩文档
            /// </summary>
            /// <param name="FileFullName"></param>
            /// <param name="FolderName">压缩文档中的文件夹，支持多级创建：abc\\def\\</param>
            public void AddFileToFolder(string FileFullName, string FolderName)
            {
                string name = FileFullName.Substring(FileFullName.LastIndexOf("\\") + 1);
                if (FolderName.IndexOf("/") >= 0)
                {
                    FolderName = FolderName.Replace("/", "\\");
                }
                if (!FolderName.EndsWith("\\"))
                {
                    FolderName += "\\";
                }
                this._z.Add(FileFullName, FolderName + name);
            }
            public void AddStream(Stream Stream, string EntryName)
            {
                byte[] ss = null;
            }
            /// <summary>
            /// 添加目录到压缩文档
            /// </summary>
            /// <param name="Folder">目录的完整路径</param>
            public void AddDirectory(string Folder)
            {
                DirectoryInfo di = new DirectoryInfo(Folder);
                GetFilesToZip(di.GetFileSystemInfos(), this._z);
            }

            private void GetFilesToZip(FileSystemInfo[] fileSystemInfosToZip, ZipFile z)
            {
                //check whether the objects are null
                if (fileSystemInfosToZip != null && z != null)
                {
                    //iterate thru all the filesystem info objects
                    foreach (FileSystemInfo fi in fileSystemInfosToZip)
                    {
                        //check if it is a directory
                        if (fi is DirectoryInfo)
                        {
                            DirectoryInfo di = (DirectoryInfo)fi;
                            //add the directory
                            z.AddDirectory(di.FullName);
                            //drill thru the directory to get all
                            //the files and folders inside it.
                            GetFilesToZip(di.GetFileSystemInfos(), z);
                        }
                        else
                        {
                            //add it
                            z.Add(fi.FullName);
                        }
                    }
                }
            }

            /// <summary>
            /// 添加注释到压缩文档
            /// </summary>
            /// <param name="comment">注释内容</param>
            public void SetComment(string comment)
            {
                this._z.SetComment(comment);
            }
            public void Flush()
            {
                this._z.CommitUpdate();
            }
            public void Close()
            {
                this.Flush();
                this._z.Close();
            }

        }

        public class ZipStream
        {
            private ZipOutputStream _z = null;
            public ZipStream(Stream OutPutStream)
            {
                this._z = new ZipOutputStream(OutPutStream);
            }

            public void Write(Stream InPutStream, string EntryName)
            {
                ZipEntry ze = new ZipEntry(EntryName);
                this._z.PutNextEntry(ze);
                int len = 256;
                byte[] buffer = new Byte[len];
                int bufferLen = InPutStream.Read(buffer, 0, len);
                while (bufferLen > 0)
                {
                    this._z.Write(buffer, 0, buffer.Length);
                    bufferLen = InPutStream.Read(buffer, 0, len);
                }
                this._z.CloseEntry();
            }
            public void Write(byte[] InPutBytes, string EntryName)
            {
                ZipEntry ze = new ZipEntry(EntryName);
                this._z.PutNextEntry(ze);
                this._z.Write(InPutBytes, 0, InPutBytes.Length);
                this._z.CloseEntry();
            }
            /// <summary>
            /// 设置压缩级别 1~9
            /// </summary>
            /// <param name="Level">压缩级别 1~9</param>
            public void SetLevel(int Level)
            {
                this._z.SetLevel(Level);
            }

            public void SetPassword(string pwd)
            {
                this._z.Password = pwd;
            }

            public void SetComment(string comment)
            {
                this._z.SetComment(comment);
            }

            public void Close()
            {
                this._z.Flush();
                this._z.Finish();
                this._z.Close();
            }
        }
    }
}
